<script setup lang="ts" name="App">
import { webSocketStore } from "@/store/modules/webSocket";
import { onBeforeMount, ref } from "vue";
import {chatInfoStore} from "@/store/modules/chatInfo";
import {systemInfoStore} from "@/store/modules/systemInfo";
import {WebSocketClient} from "@/utils/WebSocketClient";

const socketStore = webSocketStore();
const chatStore = chatInfoStore();
const systemStore = systemInfoStore();
const webSocketUrl = ref(`ws://${location.host}${import.meta.env.VITE_BASE_API}/websocket/chat/${chatStore.chatUser.chatUserId}-${chatStore.chatUser.chatUserName}`);


onBeforeMount(() => {
  if (typeof WebSocket === "undefined") {
    console.error("您的浏览器不支持socket");
    return;
  }
  if (socketStore.webSocketClient && socketStore.webSocketClient.ws?.readyState === WebSocket.OPEN) {
    return;
  }
  const webSocketClient = new WebSocketClient(webSocketUrl.value, chatStore, systemStore);
  webSocketClient.connect();
  socketStore.webSocketClient = webSocketClient;

});

</script>

<template>
  <router-view />
</template>

<style scoped>
</style>
